<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .active {
      color: red;
    }
  </style>
</head>

<body>
  <div id="app"></div>

  <template id="my-app">
    <!-- 对象语法 -->
    <h2 :class="{'active': isActive}">哈哈哈</h2>
    <button @click="toggle">切换</button>

    <!-- 也可以有多个键值对 -->
    <div :class="{active: isActive, title: true}">哈哈哈</div>

    <!-- 默认的class和动态的class结合 -->
    <div class="abc cba" :class="{active: isActive, title: true}">哈哈哈</div>

    <!-- 将对象放到一个单独的属性中 -->
    <div class="abc cba" :class="classObj">哈哈哈</div>

    <!-- 将返回的对象放到一个methods、计算属性中 -->
    <div class="abc cba" :class="getClassObj()">哈哈哈</div>
  </template>

  <script src="../js/vue.js"></script>
  <script>
    const App = {
      template: "#my-app",
      data() {
        return {
          className: "why",
          isActive: true,
          title: "abc",
          classObj: {
            active: true,
            title: true,
          }
        }
      },
      methods: {
        toggle() {
          this.isActive = !this.isActive;
        },
        getClassObj() {
          return {
            active: true,
            title: true,
          }
        },
      }
    };

    Vue.createApp(App).mount("#app");
  </script>
</body>

</html>